'\" et
.TH SEMCTL "3P" 2017 "IEEE/The Open Group" "POSIX Programmer's Manual"
.\"
.SH PROLOG
This manual page is part of the POSIX Programmer's Manual.
The Linux implementation of this interface may differ (consult
the corresponding Linux manual page for details of Linux behavior),
or the interface may not be implemented on Linux.
.\"
.SH NAME
semctl
\(em XSI semaphore control operations
.SH SYNOPSIS
.LP
.nf
#include\ <sys/sem.h>
.P
int semctl(int \fIsemid\fP, int \fIsemnum\fP, int \fIcmd\fP, ...);
.fi
.SH DESCRIPTION
The
\fIsemctl\fR()
function operates on XSI semaphores (see the Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 4.17" ", " "Semaphore").
It is unspecified whether this function interoperates with the
realtime interprocess communication facilities defined in
.IR "Section 2.8" ", " "Realtime".
.P
The
\fIsemctl\fR()
function provides a variety of semaphore control operations as
specified by
.IR cmd .
The fourth argument is optional and depends upon the operation
requested. If required, it is of type
.BR "union semun" ,
which the application shall explicitly declare:
.sp
.RS 4
.nf

union semun {
    int val;
    struct semid_ds *buf;
    unsigned short  *array;
} arg;
.fi
.P
.RE
.P
Each operation shall be performed atomically.
.P
The following semaphore control operations as specified by
.IR cmd
are executed with respect to the semaphore specified by
.IR semid
and
.IR semnum .
The level of permission required for each operation is shown with each
command; see
.IR "Section 2.7" ", " "XSI Interprocess Communication".
The symbolic names for the values of
.IR cmd
are defined in the
.IR <sys/sem.h> 
header:
.IP GETVAL 12
Return the value of
.IR semval ;
see
.IR <sys/sem.h> .
Requires read permission.
.IP SETVAL 12
Set the value of
.IR semval
to
.IR arg.val ,
where
.IR arg
is the value of the fourth argument to
\fIsemctl\fR().
When this command is successfully executed, the
.IR semadj
value corresponding to the specified semaphore in all processes is
cleared. Also, the
.IR sem_ctime
timestamp shall be set to the current time, as described in
.IR "Section 2.7.1" ", " "IPC General Description".
Requires alter permission; see
.IR "Section 2.7" ", " "XSI Interprocess Communication".
.IP GETPID 12
Return the value of
.IR sempid .
Requires read permission.
.IP GETNCNT 12
Return the value of
.IR semncnt .
Requires read permission.
.IP GETZCNT 12
Return the value of
.IR semzcnt .
Requires read permission.
.P
The following values of
.IR cmd
operate on each
.IR semval
in the set of semaphores:
.IP GETALL 12
Return the value of
.IR semval
for each semaphore in the semaphore set and place into the array
pointed to by
.IR arg.array ,
where
.IR arg
is the fourth argument to
\fIsemctl\fR().
Requires read permission.
.IP SETALL 12
Set the value of
.IR semval
for each semaphore in the semaphore set according to the array pointed
to by
.IR arg.array ,
where
.IR arg
is the fourth argument to
\fIsemctl\fR().
When this command is successfully executed, the
.IR semadj
values corresponding to each specified semaphore in all processes are
cleared. Also, the
.IR sem_ctime
timestamp shall be set to the current time, as described in
.IR "Section 2.7.1" ", " "IPC General Description".
Requires alter permission.
.br
.P
The following values of
.IR cmd
are also available:
.IP IPC_STAT 12
Place the current value of each member of the
.BR semid_ds
data structure associated with
.IR semid
into the structure pointed to by
.IR arg.buf ,
where
.IR arg
is the fourth argument to
\fIsemctl\fR().
The contents of this structure are defined in
.IR <sys/sem.h> .
Requires read permission.
.IP IPC_SET 12
Set the value of the following members of the
.BR semid_ds
data structure associated with
.IR semid
to the corresponding value found in the structure pointed to by
.IR arg.buf ,
where
.IR arg
is the fourth argument to
\fIsemctl\fR():
.RS 12 
.sp
.RS 4
.nf

sem_perm.uid
sem_perm.gid
sem_perm.mode
.fi
.P
.RE
.P
The mode bits specified in
.IR "Section 2.7.1" ", " "IPC General Description"
are copied into the corresponding bits of the
.IR sem_perm.mode
associated with
.IR semid .
The stored values of any other bits are unspecified. The
.IR sem_ctime
timestamp shall be set to the current time, as described in
.IR "Section 2.7.1" ", " "IPC General Description".
.P
This command can only be executed by a process that has an effective
user ID equal to either that of a process with appropriate privileges
or to the value of
.IR sem_perm.cuid
or
.IR sem_perm.uid
in the
.BR semid_ds
data structure associated with
.IR semid .
.RE
.IP IPC_RMID 12
Remove the semaphore identifier specified by
.IR semid
from the system and destroy the set of semaphores and
.BR semid_ds
data structure associated with it. This command can only be executed
by a process that has an effective user ID equal to either that of a
process with appropriate privileges or to the value of
.IR sem_perm.cuid
or
.IR sem_perm.uid
in the
.BR semid_ds
data structure associated with
.IR semid .
.SH "RETURN VALUE"
If successful, the value returned by
\fIsemctl\fR()
depends on
.IR cmd
as follows:
.IP GETVAL 12
The value of
.IR semval .
.IP GETPID 12
The value of
.IR sempid .
.IP GETNCNT 12
The value of
.IR semncnt .
.IP GETZCNT 12
The value of
.IR semzcnt .
.IP "All others" 12
0.
.P
Otherwise,
\fIsemctl\fR()
shall return \-1 and set
.IR errno
to indicate the error.
.SH ERRORS
The
\fIsemctl\fR()
function shall fail if:
.TP
.BR EACCES
Operation permission is denied to the calling process; see
.IR "Section 2.7" ", " "XSI Interprocess Communication".
.TP
.BR EINVAL
The value of
.IR semid
is not a valid semaphore identifier, or the value of
.IR semnum
is less than 0 or greater than or equal to
.IR sem_nsems ,
or the value of
.IR cmd
is not a valid command.
.TP
.BR EPERM
The argument
.IR cmd
is equal to IPC_RMID or IPC_SET
and the effective user ID of the calling process is not equal to that
of a process with appropriate privileges and it is not equal to the
value of
.IR sem_perm.cuid
or
.IR sem_perm.uid
in the data structure associated with
.IR semid .
.TP
.BR ERANGE
The argument
.IR cmd
is equal to SETVAL or SETALL and the value to which
.IR semval
is to be set is greater than the system-imposed maximum.
.LP
.IR "The following sections are informative."
.SH EXAMPLES
Refer to
.IR "\fIsemop\fR\^(\|)".
.SH "APPLICATION USAGE"
The fourth parameter in the SYNOPSIS section is now specified as
.BR \(dq...\(dq 
in order to avoid a clash with the ISO\ C standard when referring to the union
.IR semun
(as defined in Issue 3) and for backwards-compatibility.
.P
The POSIX Realtime Extension defines alternative interfaces for interprocess
communication. Application developers who need to use IPC should
design their applications so that modules using the IPC routines
described in
.IR "Section 2.7" ", " "XSI Interprocess Communication"
can be easily modified to use the alternative interfaces.
.SH RATIONALE
None.
.SH "FUTURE DIRECTIONS"
None.
.SH "SEE ALSO"
.ad l
.IR "Section 2.7" ", " "XSI Interprocess Communication",
.IR "Section 2.8" ", " "Realtime",
.IR "\fIsemget\fR\^(\|)",
.IR "\fIsemop\fR\^(\|)",
.IR "\fIsem_close\fR\^(\|)",
.IR "\fIsem_destroy\fR\^(\|)",
.IR "\fIsem_getvalue\fR\^(\|)",
.IR "\fIsem_init\fR\^(\|)",
.IR "\fIsem_open\fR\^(\|)",
.IR "\fIsem_post\fR\^(\|)",
.IR "\fIsem_trywait\fR\^(\|)",
.IR "\fIsem_unlink\fR\^(\|)"
.ad b
.P
The Base Definitions volume of POSIX.1\(hy2017,
.IR "Section 4.17" ", " "Semaphore",
.IR "\fB<sys_sem.h>\fP"
.\"
.SH COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form
from IEEE Std 1003.1-2017, Standard for Information Technology
-- Portable Operating System Interface (POSIX), The Open Group Base
Specifications Issue 7, 2018 Edition,
Copyright (C) 2018 by the Institute of
Electrical and Electronics Engineers, Inc and The Open Group.
In the event of any discrepancy between this version and the original IEEE and
The Open Group Standard, the original IEEE and The Open Group Standard
is the referee document. The original Standard can be obtained online at
http://www.opengroup.org/unix/online.html .
.PP
Any typographical or formatting errors that appear
in this page are most likely
to have been introduced during the conversion of the source files to
man page format. To report such errors, see
https://www.kernel.org/doc/man-pages/reporting_bugs.html .
